﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class Operation
    {
        #region Variables
        private string name;

        private string description;

        private bool isActive;

        private LinkedList<Action> actionSet;

      
        #endregion 

        #region Properties
        public bool IsActive
        {
            get { return isActive; }
            set { isActive = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public LinkedList<Action> ActionSet
        {
            get { return actionSet; }
            set { actionSet = value; }  // Vanya edit
        }
        #endregion

        #region Constructors
        public Operation(string name, string description, bool isActive)
        {
            this.name = name;
            this.description = description;
            this.isActive = isActive;
            actionSet = new LinkedList<Action>();
        }
        #endregion

        #region Implementation
        public void Perform()
        {
            if (isActive)
            {
                foreach (var action in actionSet)
                    action.Execute();
            }
        }

        public void AddAction(Action param)
        {
            actionSet.AddLast(param);
        }

        public bool RemoveAction(Action param)
        {
            return actionSet.Remove(param);
        }

        public void MoveActionDown(Action param)
        {
            LinkedListNode<Action> foundedItem = actionSet.Find(param);
            if (foundedItem != null)
            {
#warning need to test
                LinkedListNode<Action> tmp = new LinkedListNode<Action>(null);
                tmp.Value= foundedItem.Next.Value;
                actionSet.Remove(foundedItem.Next);
                actionSet.AddBefore(foundedItem, tmp.Value);
            }   
            else
            {
                throw new ArgumentException();
            }
        }

        public void MoveActionUp(Action param)
        {
            LinkedListNode<Action> foundedItem = actionSet.Find(param);
            if (foundedItem != null)
            {
#warning need to test
                LinkedListNode<Action> tmp = new LinkedListNode<Action>(null);
                tmp.Value = foundedItem.Previous.Value;
                actionSet.Remove(foundedItem.Previous);
                actionSet.AddAfter(foundedItem, tmp.Value);
            }
            else
            {
                throw new ArgumentException();
            }
        }
        #endregion
    }
}
